﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using kim.Common;

namespace kim.Web.flf0624.download
{
    public partial class AddorEdit : System.Web.UI.Page
    {
        protected string type;
        protected int typeid, fid;
        private int Nid;
        protected void Page_Load(object sender, EventArgs e)
        {
            type = KIMRequest.GetString("type");
            typeid = KIMRequest.GetInt("typeid",1);
            Nid = KIMRequest.GetInt("id", 1);
            if (!IsPostBack)
                GetRequestValues();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (type == KIMEnums.ActionEnum.Edit.ToString())
            {
                if (DoEdit(Nid))
                    lbmsg.Text = "修改成功";
                else
                    lbmsg.Text = "修改失败";
            }
            if (type == KIMEnums.ActionEnum.Add.ToString())
            {
                if (DoAdd())
                    lbmsg.Text = "添加成功";
                else
                    lbmsg.Text = "添加失败";
            }

        }

        #region 编辑添加基本操作=========================================

        private bool DoEdit(int id)
        {
            Model.download_attach Mnew = new Model.download_attach();
            Mnew.file_path = hfFile.Value;
            Mnew.title = this.txtTitle.Value;
            Mnew.Sort = int.Parse(txt_postion.Value);
            Mnew.id = id;
            Mnew.typeid = this.typeid;
            return new BLL.download_attach().Update(Mnew);
        }

        private bool DoAdd()
        {
            Model.download_attach Mnew = new Model.download_attach();
            Mnew.file_path = hfFile.Value;
            Mnew.title = this.txtTitle.Value;
            Mnew.typeid = this.typeid;
            Mnew.Sort = int.Parse( txt_postion.Value);
            BLL.download_attach bll = new BLL.download_attach();
            if (bll.Add(Mnew) > 0)
                return true;
            else
                return false;
        }
        #endregion

        private void GetRequestValues()
        {

            if (type == KIMEnums.ActionEnum.Edit.ToString())
            {
                GetEditValue();
            }
            if (type == KIMEnums.ActionEnum.Delete.ToString())
            {
                bool del = new BLL.download_attach().Delete(Nid);
                if (del)
                    Response.Redirect("List.aspx?typeid=" + typeid );
            }
            if (type != KIMEnums.ActionEnum.Edit.ToString() && type != KIMEnums.ActionEnum.Add.ToString() && type != KIMEnums.ActionEnum.Delete.ToString())
                Response.Redirect("../indexpage.aspx");


        }

        private void GetEditValue()
        {
            Model.download_attach model = new BLL.download_attach().GetModel(Nid);
            this.txtTitle.Value = model.title;
            this.hfFile.Value = model.file_path;

        }

        protected void htn_upload_Click(object sender, EventArgs e)//上传
        {
            string strpic = PubFileAndImageFunc.fileuploadcoderar(this.FileUpload1, "/uploadfile/doc/");
            this.lblFile.Text = this.FileUpload1.FileName;
           
            this.hfFile.Value = strpic;
        }
    }
}